package com.monitor.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerIntegral;
import com.monitor.entity.ErpUser;
import com.monitor.service.ErpCustomerCarService;
import com.monitor.service.ErpCustomerIntegralService;
import com.monitor.service.ErpCustomerService;
import com.monitor.service.ErpUserService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
@RestController
@RequestMapping("/erpCustomer")
@Api(description="客户")
public class ErpCustomerController {

    @Resource
    private ErpCustomerService customerService;
    @Resource
    private ErpUserService userService;
    @Resource
    private ErpCustomerIntegralService integralService;
    
    @RequestMapping("/getErpCustor")
    @ApiOperation("客户信息普通查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "客户名称|编码", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody Result<Map<String,Object>> getErpCustor( Integer userId) {
        Result<Map<String,Object>> result=new Result<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Integer> numMap=new HashMap<String,Integer>();
        try{
            ErpUser erpUser=userService.selectById(userId);
            ErpCustomer customers=new ErpCustomer();

            List<ErpCustomer> customers1=new ArrayList<ErpCustomer>();
//            if(StringUtils.isEmpty(code)){
//                customers=customerService.selectById(erpUser.getCustomerId());
//                customers1=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customers.getCusCode()));
//                if(null!=customers){
//                    customers1.add(customers);
//                }
//            }else{
                customers=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",erpUser.getCustomerId()));
                ErpCustomer customer=customerService.selectById(erpUser.getCustomerId());
                customers1=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()));
                if(null!=customers){
                    customers1.add(0, customers);
                }
//            }

            Integer customerNum=customers1.size();
                customer.setCusVehicletotal(0);
            Integer carNum=customers==null?0:customerService.selectVehicletotalCount(customers.getCusCode())+customers.getCusVehicletotal();
            Integer psacarnum=0;
            Integer xcarnum=0;
            Integer dcarnum=0;
            Integer gcarnum=0;
            Integer tcarnum=0;
            Integer groupNum=0;
            Integer vIPNum=0;
            Integer goldNum=0;
            for (ErpCustomer customer1:customers1) {
                carNum+=customers.getCusVehicletotal();
                Map<String, BigDecimal> map1=customerService.getCustomerCarNum(customer1.getCusCode());
                if(null!=map1){
                    int psacarnum3=map1.get("psacarnum").intValue();
                    customer.setCusPsacar(null==map1.get("psacarnum")||0==map1.get("psacarnum").intValue()?customer.getCusPsacar():customer.getCusPsacar()+map1.get("psacarnum").intValue());
                    customer.setCusXCar(null==map1.get("xcarnum")||0==map1.get("xcarnum").intValue()?customer.getCusXCar():customer.getCusXCar()+map1.get("xcarnum").intValue());
                    customer.setCusDCar(null==map1.get("dcarnum")||0==map1.get("dcarnum").intValue()?customer.getCusDCar():customer.getCusDCar()+map1.get("dcarnum").intValue());
                    customer.setCusGCar(null==map1.get("gcarnum")||0==map1.get("gcarnum").intValue()?customer.getCusGCar():customer.getCusGCar()+map1.get("gcarnum").intValue());
                    customer.setCusTCar(null==map1.get("tcarnum")||0==map1.get("tcarnum").intValue()?customer.getCusTCar():customer.getCusTCar()+map1.get("tcarnum").intValue());
                }
                psacarnum+=customer.getCusPsacar();
                xcarnum+=customer.getCusXCar();
                dcarnum+=customer.getCusDCar();
                gcarnum+=customer.getCusGCar();
                tcarnum+=customer.getCusTCar();
                Integer groupNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().isNull("cus_sup_company").and().eq("cus_cus_type","集团客户"));
                Integer vIPNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().isNull("cus_sup_company").and().eq("cus_cus_type","VIP客户"));
                Integer goldNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().isNull("cus_sup_company").and().eq("cus_cus_type","金牌客户"));
                if(customer.getCusCusType().equals("集团客户")){
                    groupNum1+=1;
                }
                if(customer.getCusCusType().equals("VIP客户")){
                    vIPNum1+=1;
                }
                if(customer.getCusCusType().equals("金牌客户")){
                    goldNum1+=1;
                }
                customer.setGroupNum(groupNum1);
                customer.setvIPNum(vIPNum1);
                customer.setGoldNum(goldNum1);
                groupNum+=groupNum1;vIPNum+=vIPNum1;goldNum+=goldNum1;
            }
            numMap.put("psacarnum",psacarnum);
            numMap.put("xcarnum",xcarnum);
            numMap.put("dcarnum",dcarnum);
            numMap.put("gcarnum",gcarnum);
            numMap.put("tcarnum",tcarnum);
            map.put("customerNum",customerNum);
            map.put("carNum",carNum);
            map.put("numMap",numMap);
            map.put("customerList",customers1);
            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }
    @RequestMapping("/getLikeErpCustor")
    @ApiOperation("客户信息通过编码和名字模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "客户名称|编码", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody Result<Map<String,Object>> getLikeErpCustor(String code, Integer userId) {
        Result<Map<String,Object>> result=new Result<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Integer> numMap=new HashMap<String,Integer>();
        try{
            ErpUser erpUser=userService.selectById(userId);
            ErpCustomer customers=new ErpCustomer();

            List<ErpCustomer> customers1=new ArrayList<ErpCustomer>();
            customers=customerService.selectById(erpUser.getCustomerId());
            if(StringUtils.isEmpty(code)){
                
                customers1=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customers.getCusCode()));
                if(null!=customers){
                    customers1.add(customers);
                }
            }else{
                customers=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",erpUser.getCustomerId()).and().like("cus_code",code).or().like("cus_name",code).and().eq("id",erpUser.getCustomerId()));
                ErpCustomer customer=customerService.selectById(erpUser.getCustomerId());
                customers1=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().like("cus_code",code).or().like("cus_name",code).and().eq("cus_sup_company",customer.getCusCode()));
                if(null!=customers){
                    customers1.add(customers);
                }
            }
            Collections.reverse(customers1);


            Integer carNum=customers==null?0:customerService.selectVehicletotalCount(customers.getCusCode())+customers.getCusVehicletotal();
            Integer psacarnum=0;
            Integer xcarnum=0;
            Integer dcarnum=0;
            Integer gcarnum=0;
            Integer tcarnum=0;
            Integer groupNum=0;
            Integer vIPNum=0;
            Integer goldNum=0;
            for (ErpCustomer customer:customers1) {
                if(null!=customers){
                    carNum+=customers.getCusVehicletotal();
                }
                Map<String, BigDecimal> map1=customerService.getCustomerCarNum(customer.getCusCode());
                if(null!=map1){
                    int psacarnum3=map1.get("psacarnum").intValue();
                    customer.setCusPsacar(null==map1.get("psacarnum")||0==map1.get("psacarnum").intValue()?customer.getCusPsacar():customer.getCusPsacar()+map1.get("psacarnum").intValue());
                    customer.setCusXCar(null==map1.get("xcarnum")||0==map1.get("xcarnum").intValue()?customer.getCusXCar():customer.getCusXCar()+map1.get("xcarnum").intValue());
                    customer.setCusDCar(null==map1.get("dcarnum")||0==map1.get("dcarnum").intValue()?customer.getCusDCar():customer.getCusDCar()+map1.get("dcarnum").intValue());
                    customer.setCusGCar(null==map1.get("gcarnum")||0==map1.get("gcarnum").intValue()?customer.getCusGCar():customer.getCusGCar()+map1.get("gcarnum").intValue());
                    customer.setCusTCar(null==map1.get("tcarnum")||0==map1.get("tcarnum").intValue()?customer.getCusTCar():customer.getCusTCar()+map1.get("tcarnum").intValue());
                }
                psacarnum+=customer.getCusPsacar();
                xcarnum+=customer.getCusXCar();
                dcarnum+=customer.getCusDCar();
                gcarnum+=customer.getCusGCar();
                tcarnum+=customer.getCusTCar();
                Integer groupNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","集团客户"));
                Integer vIPNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","VIP客户"));
                Integer goldNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","金牌客户"));
                if(customer.getCusCusType().equals("集团客户")){
                    groupNum1+=1;
                }
                if(customer.getCusCusType().equals("VIP客户")){
                    vIPNum1+=1;
                }
                if(customer.getCusCusType().equals("金牌客户")){
                    goldNum1+=1;
                }
                customer.setGroupNum(groupNum1);
                customer.setvIPNum(vIPNum1);
                customer.setGoldNum(goldNum1);
                groupNum+=groupNum1;vIPNum+=vIPNum1;goldNum+=goldNum1;
            }
            Integer customerNum=groupNum+vIPNum+goldNum;
            numMap.put("psacarnum",psacarnum);
            numMap.put("xcarnum",xcarnum);
            numMap.put("dcarnum",dcarnum);
            numMap.put("gcarnum",gcarnum);
            numMap.put("tcarnum",tcarnum);
            map.put("customerNum",customerNum);
            map.put("carNum",carNum);
            map.put("numMap",numMap);
            map.put("customerList",customers1);
            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }
    @RequestMapping("/getErpCustorByCustomerId")
    @ApiOperation("根据客户id进一步查看类型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<Map<String,Object>> getErpCustorByCustomerId(Integer customerId) {
        Result<Map<String,Object>> result=new Result<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Integer> numMap=new HashMap<String,Integer>();
        try{
            ErpCustomer customers=customerService.selectById(customerId);
            List<ErpCustomer> customers1=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customers.getCusCode()));
            Integer carNum=0;
            Integer groupNum=0;
            Integer vIPNum=0;
            Integer goldNum=0;
            Integer groupCarNum=0;
            Integer vIPCarNum=0;
            Integer goldCarNum=0;
            Map<String,BigDecimal> map1=customerService.getCustomerCarNum(customers.getCusCode());
            for (ErpCustomer customer:customers1) {
                carNum+=customer.getCusVehicletotal();

                Integer groupNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","集团客户"));
                Integer vIPNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","VIP客户"));
                Integer goldNum1=customerService.selectCount(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customer.getCusCode()).and().eq("cus_cus_type","金牌客户"));
                if(customer.getCusCusType().equals("集团客户")){
                    groupNum+=1;
                    //groupMap.add(customer);
                    groupCarNum+=customer.getCusVehicletotal();
                }
                if(customer.getCusCusType().equals("VIP客户")){
                    vIPNum+=1;
                    vIPCarNum+=customer.getCusVehicletotal();
                }
                if(customer.getCusCusType().equals("金牌客户")){
                    goldNum+=1;
                    goldCarNum+=customer.getCusVehicletotal();
                }
                groupNum+=groupNum1;vIPNum+=vIPNum1;goldNum+=goldNum1;
            }
            numMap.put("psacarnum",customers.getCusPsacar()+map1.get("psacarnum").intValue());
            numMap.put("xcarnum",customers.getCusXCar()+map1.get("xcarnum").intValue());
            //numMap.put("xcarnum",customers.getCusXCar()+map1.get("xcarnum"));
            numMap.put("dcarnum",customers.getCusDCar()+map1.get("dcarnum").intValue());
            numMap.put("gcarnum",customers.getCusGCar()+map1.get("gcarnum").intValue());
            numMap.put("tcarnum",customers.getCusTCar()+map1.get("tcarnum").intValue());
            customers1.add(customers);
            Integer customerNum=customers1.size();//客户总数
            map.put("customerNum",customerNum);
            map.put("carNum",carNum);
            map.put("numMap",numMap);
            Map<String,Object> groupMap1=new HashMap<String,Object>();
            Map<String,Object> vipMap1=new HashMap<String,Object>();
            Map<String,Object> goidMap1=new HashMap<String,Object>();
            Map<String,Object> groupMap=new HashMap<String,Object>();
            Map<String,Object> vipMap=new HashMap<String,Object>();
            Map<String,Object> goidMap=new HashMap<String,Object>();
            groupMap=customerService.getCustomerCarNumByType(customers.getCusCode(),"集团客户");
            vipMap=customerService.getCustomerCarNumByType(customers.getCusCode(),"VIP客户");
            goidMap=customerService.getCustomerCarNumByType(customers.getCusCode(),"金牌客户");
            if(customers.getCusCusType().equals("集团客户")){
                groupNum+=1;
                groupCarNum+=customers.getCusVehicletotal();
                if(0==groupMap.size()){
                    groupMap.put("psacarnum",customers.getCusPsacar());
                    groupMap.put("xcarnum",customers.getCusXCar());
                    groupMap.put("dcarnum",customers.getCusDCar());
                    groupMap.put("gcarnum",customers.getCusGCar());
                    groupMap.put("tcarnum",customers.getCusTCar());
                }else{
                    groupMap.put("psacarnum",customers.getCusPsacar()+new BigDecimal(groupMap.get("psacarnum").toString()).intValue());
                    groupMap.put("xcarnum",customers.getCusXCar()+new BigDecimal(groupMap.get("xcarnum").toString()).intValue());
                    groupMap.put("dcarnum",customers.getCusDCar()+new BigDecimal(groupMap.get("dcarnum").toString()).intValue());
                    groupMap.put("gcarnum",customers.getCusGCar()+new BigDecimal(groupMap.get("gcarnum").toString()).intValue());
                    groupMap.put("tcarnum",customers.getCusTCar()+new BigDecimal(groupMap.get("tcarnum").toString()).intValue());
                }

            }
            if(customers.getCusCusType().equals("VIP客户")){
                vIPNum+=1;
                vIPCarNum+=customers.getCusVehicletotal();
                if(vipMap==null){
                	vipMap=new HashMap<String,Object>();
                    vipMap.put("psacarnum",customers.getCusPsacar());
                    vipMap.put("xcarnum",customers.getCusXCar());
                    vipMap.put("dcarnum",customers.getCusDCar());
                    vipMap.put("gcarnum",customers.getCusGCar());
                    vipMap.put("tcarnum",customers.getCusTCar());
                }else{

                    vipMap.put("psacarnum",customers.getCusPsacar()+new BigDecimal(vipMap.get("psacarnum").toString()).intValue());
                    vipMap.put("xcarnum",customers.getCusXCar()+new BigDecimal(vipMap.get("xcarnum").toString()).intValue());
                    vipMap.put("dcarnum",customers.getCusDCar()+new BigDecimal(vipMap.get("dcarnum").toString()).intValue());
                    vipMap.put("gcarnum",customers.getCusGCar()+new BigDecimal(vipMap.get("gcarnum").toString()).intValue());
                    vipMap.put("tcarnum",customers.getCusTCar()+new BigDecimal(vipMap.get("tcarnum").toString()).intValue());
                }

            }
            if(customers.getCusCusType().equals("金牌客户")){
                goldNum+=1;
                goldCarNum+=customers.getCusVehicletotal();
                if(goidMap!=null){
                    goidMap.put("psacarnum",customers.getCusPsacar());
                    goidMap.put("xcarnum",customers.getCusXCar());
                    goidMap.put("dcarnum",customers.getCusDCar());
                    goidMap.put("gcarnum",customers.getCusGCar());
                    goidMap.put("tcarnum",customers.getCusTCar());
                }else{
                    goidMap.put("psacarnum",customers.getCusPsacar()+new BigDecimal(goidMap.get("goidMap").toString()).intValue());
                    goidMap.put("xcarnum",customers.getCusXCar()+new BigDecimal(goidMap.get("xcarnum").toString()).intValue());
                    goidMap.put("dcarnum",customers.getCusDCar()+new BigDecimal(goidMap.get("dcarnum").toString()).intValue());
                    goidMap.put("gcarnum",customers.getCusGCar()+new BigDecimal(goidMap.get("gcarnum").toString()).intValue());
                    goidMap.put("tcarnum",customers.getCusTCar()+new BigDecimal(goidMap.get("tcarnum").toString()).intValue());
                }

            }
            groupMap1.put("groupMap",groupMap==null?0:groupMap);
            groupMap1.put("groupNum",groupNum);
            groupMap1.put("groupCarNum",groupCarNum>0?groupCarNum:0);
            vipMap1.put("vipMap",vipMap==null?0:vipMap);
            vipMap1.put("vIPNum",vIPNum);
            vipMap1.put("vIPCarNum",vIPCarNum>0?vIPCarNum:0);
            goidMap1.put("goidMap",goidMap==null?0:goidMap);
            goidMap1.put("goldNum",goldNum);
            goidMap1.put("goldCarNum",goldCarNum>0?goldCarNum:0);
            map.put("groupMap",groupMap1);
            map.put("vipMap",vipMap1);
            map.put("goldMap",goidMap1);
            result.setCount(1);
            result.setData(map);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }


    @RequestMapping("/getErpCustorByCustType")
    @ApiOperation("根据客户类型查看客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "客户类型", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody Result<List<ErpCustomer>> getErpCustorByCustType(String code,Integer customerCode) {
        Result<List<ErpCustomer>> result=new Result<List<ErpCustomer>>();
        try{
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",customerCode).eq("cus_cus_type",code).isNull("cus_sup_company"));
            ErpCustomer customer=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_code",customerCode));
            if(code.equals(customer.getCusCusType())){
                customers.add(customer);
            }
            //List<ErpCustomer> customers=customerService.selectCoutomerByType(customerCode,code);
            Collections.reverse(customers);
            result.setData(customers);
            result.setCount(customers.size());
        }catch (Exception e){e.printStackTrace();}


        return result;
    }

    @RequestMapping("/getErpCustorByCustId")
    @ApiOperation("根据客户ID查看客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<Map<String, Object>> getErpCustorByCustId(Integer custId) {
        Result<Map<String, Object>> result=new Result<Map<String, Object>>();
        try{
        	Map<String, Object> map=new HashMap<>();
            ErpCustomer customers=customerService.selectById(custId);
            map.put("customers", customers);
            ErpCustomer cusServiceCom=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_code", customers.getCusServiceCom()));
            if(cusServiceCom==null) {
            	map.put("cusServiceCom", null);
            }else {
            	map.put("cusServiceCom", cusServiceCom.getCusName());
            }
            result.setData(map);
            result.setCount(1);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }



   @RequestMapping("/updateCustomer")
    @ApiOperation("修改客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
   public @ResponseBody Result<ErpCustomer> updateCustomer(ErpCustomer erpCustomer) {
       Result<ErpCustomer>  result=new Result<ErpCustomer> ();
        try{

            boolean b = customerService.updateById(erpCustomer);


            result.setData(erpCustomer);
            result.setCount(1);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }





    @RequestMapping("/inertOrUpdateCustomer")
    @ApiOperation("添加Or修改客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody Result<ErpCustomer> inertOrUpdateCustomer(ErpCustomer erpCustomer) {
    	if(erpCustomer.getCusName()==null||erpCustomer.getCusName().equals("")) {
    		return new Result<>(1, "没有数据不能保存");
    	}
        Result<ErpCustomer> result=new Result<ErpCustomer>();
        String b=erpCustomer.getId()+"";
        try{
        	Integer pan=0;


          if ((b.equals("null"))){
              erpCustomer.setCusVehicletotal(0);
              erpCustomer.setCusXCar(0);
              erpCustomer.setPassword("88888888");

              ErpCustomer erpCustomer1 = customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_code", erpCustomer.getCusSupCompany()));
	          if (null!=erpCustomer1){
	                erpCustomer1.setCusCusType("集团客户");
	                customerService.updateById(erpCustomer1);
	          }
          }

            String type=customerService.selectCustType(erpCustomer);
            erpCustomer.setCusCusType(type);
           /* ErpCustomer erpCustomer1=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_code  ",erpCustomer.getCusSupCompany()));
            if(null!=erpCustomer1){*/


           if ((b.equals("null"))){
               List<ErpCustomer> erpCustomerList = customerService.selectList(new EntityWrapper<ErpCustomer>());
               ErpCustomer erpCustomer1 = erpCustomerList.get(erpCustomerList.size() - 1);
               erpCustomer.setCusCode(erpCustomer1.getCusCode()+1);
           }
          /* if(null!=erpCustomer.getCusSupCompany()&&0!=erpCustomer.getCusSupCompany()){
               List<ErpCustomer> customerList=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",erpCustomer.getCusSupCompany()));
               if(2>=customerList.size()){
                   ErpCustomer erpCustomer1=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_code",erpCustomer.getCusSupCompany()));
                   erpCustomer1.setCusCusType("集团客户");
                   customerService.updateById(erpCustomer1);
               }
           }*/
            /*}*/
            int a=0;
            if(erpCustomer.getId()==null||erpCustomer.getId()==0) {
            	pan=1;       //如果是添加客户 值为1
	            List<ErpCustomer> erpCustomerList = customerService.selectList(new EntityWrapper<ErpCustomer>());
	         c:   for (int i = 0; i < erpCustomerList.size(); i++) {
	                ErpCustomer customer = erpCustomerList.get(i);
	                if(customer.getCusName()!=null) {
		                if (customer.getCusName().equals(erpCustomer.getCusName())) {
		                    a = 1;
		                    break c;
		                } else {
		                    a = 0;
		                }
		            }else {
		            	 a = 1;
		                 break c;
		            }
		         }
	            }
                if (a == 0) {
                	
                    customerService.insertOrUpdate(erpCustomer);
                    if(pan==1) {
	                    ErpCustomerIntegral erpCustomerIntegral=new ErpCustomerIntegral();
	                    ErpUser erpUser =new ErpUser();
	                    erpUser.setStatus(1);
	                    erpUser.setPhone(erpCustomer.getCusCode()+"");
	                    erpUser.setCustomerId(erpCustomer.getId());
	                    erpUser.setPassword("88888888");
	                    erpUser.setRealName(erpCustomer.getCusLegalPerson());
	                    erpUser.setRoleId(1);
	                    erpUser.setRolesId(3);
	                    userService.insert(erpUser);
	
	                    ErpCustomerIntegral erpCustomerIntegral1 = integralService.selectOne(new EntityWrapper<ErpCustomerIntegral>().eq("customer_id", erpCustomer.getId()));
	                    if (null==erpCustomerIntegral1){
	                        erpCustomerIntegral.setCustomerId(erpCustomer.getId());
	                        
	                        integralService.insert(erpCustomerIntegral);
	                    }
                    }
                    result.setCount(1);
                }else{
                	result.setMsg("用户名重复");
                	result.setCount(0);
             		result.setCode(1);
                }



            result.setData(erpCustomer);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeCustomer")
    @ApiOperation("删除客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<Integer> removeCustomer(Integer custId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            ErpUser erpUser = userService.selectById(custId);
            userService.deleteById(custId);


            result.setData(customerService.deleteById(erpUser.getCustomerId())?1:0);
        }catch (Exception e){e.printStackTrace();}


        return result;
    }

    @RequestMapping("/getListCustomer")
    @ApiOperation("获取所有客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody Result<List<ErpCustomer>> getListCustomer() {
        Result<List<ErpCustomer>> result=new Result<List<ErpCustomer>>();
        try{
            result.setCount(1);
            result.setData(customerService.selectList(new EntityWrapper<ErpCustomer>()));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }



    @RequestMapping("/getListCustomerByCustId")
    @ApiOperation("获取客户有关的客户信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<List<ErpCustomer>> getListCustomerByCustId(Integer custId) {
        Result<List<ErpCustomer>> result=new Result<List<ErpCustomer>>();
        try{
            ErpCustomer customer=customerService.selectById(custId);
            List<ErpCustomer> customerList=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",custId).or().eq("cus_service_com",custId));
            ErpCustomer customer1=customerService.selectOne(new EntityWrapper<ErpCustomer>().like("cus_name","零星客户"));

            customerList.add(customer);
            customerList.add(customer1);

            result.setCount(customerList.size());
            result.setData(customerList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

   @RequestMapping("/selectUserCustomer")
    @ApiOperation("查询用户以下的有关客户,系统管理-用户管理")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "userId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody Result<List<ErpCustomer>> selectUserCustomer(Integer userId) {
            Result<List<ErpCustomer>> result=new Result<List<ErpCustomer>>();
            try{

//            result.setCount(customerList.size());
            result.setData(customerService.selectList(new EntityWrapper<ErpCustomer>().eq("create_user",userId)));
            result.setMsg("查询成功");
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
  @RequestMapping("/updateLogo")
    @ApiOperation("修改用户Logo")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "id", value = "客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "logo", value = "用户logo", required = false, dataType = "String", paramType = "query")
    })
    public Result<Integer> updateLogo(Integer id,String logo) {
      Result<Integer> result=new Result<Integer>();
            try{

//            result.setCount(customerList.size());
            result.setData(customerService.updateLogo(logo,id));
            result.setMsg("修改客户logo成功");
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

  @RequestMapping("/selectByCusNameLike")
  @ApiOperation("根据客户名检索客户列表")
  @ApiImplicitParam(name = "word", value = "检索内容（客户名）", required = false, dataType = "String", paramType = "query")
  public Result<List<ErpCustomer>> selectByCusNameLike(@RequestParam(value="word",defaultValue="")String word){
	  return customerService.selectByCusNameLike(word);
  }

    @RequestMapping("/getCustByNameLike")
    @ApiOperation("根据客户名模糊搜索客户列表")
    @ApiImplicitParam(name = "word", value = "检索内容（客户名）", required = false, dataType = "String", paramType = "query")
    public Result<List<ErpCustomer>> getCustByNameLike(String word){
        Result<List<ErpCustomer>> result=new Result<List<ErpCustomer>>();
        List<ErpCustomer> list=customerService.selectList(new EntityWrapper<ErpCustomer>().like("cus_name",word));
        result.setData(list);
        result.setCount(list.size());
        return result;
    }

 @RequestMapping("/updateCustState")
    @ApiOperation("修改客户状态")
    public Result<Integer> getCustByNameLike(ErpCustomer erpCustomer){
     Result<Integer> result=new Result<>();
        try{
            result.setData(         customerService.updateById(erpCustomer)?1:0);

        }catch(Exception e){
            e.printStackTrace();
     }
        return result;
    }
 @RequestMapping("/selectCustomerDownById")
    @ApiOperation("根据客户id查询下级单位")
    public Result<List<ErpCustomer>> selectCustomerDownById(Integer id){
     Result<List<ErpCustomer>> result=new Result<>();
        try{
            ErpCustomer erpCustomer = customerService.selectById(id);
            result.setData(customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",erpCustomer.getCusCode())));

        }catch(Exception e){
            e.printStackTrace();
     }
        return result;
    }


 	@RequestMapping("/getLikeCuster")
 	public Result<List<ErpCustomer>> getLikeCuster(int custId,@RequestParam(value="code",defaultValue="")String code){
 		 ErpCustomer erpCustomer = customerService.selectById(custId);
 		if(!code.equals("")) {
 			List<ErpCustomer> list=customerService.selectList(new EntityWrapper<ErpCustomer>().like("cus_name", code).eq("cus_service_com", erpCustomer.getCusCode()));
 			if(erpCustomer.getCusName().indexOf(code)!=-1) {
 				list.add(erpCustomer);
 			}
 			return new Result<List<ErpCustomer>>(list, list.size());
 		}else {
 			return new Result<>(1, "无");
 		}
 	}
}

